﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Q7.BevTree.Test
{
    enum EObjectiveState
    {
        /// <summary>
        /// 目标数据没有变化
        /// </summary>
        UnChanged = 0,
        /// <summary>
        /// 目标数据有更新
        /// </summary>
        Updated,
        /// <summary>
        /// 目标完成
        /// </summary>
        Finished
    }

    #region 接任务
    /// <summary>
    /// 接任务的条件
    /// </summary>
    class Condition : BevNode
    {
        /// <summary>
        /// 是否可以接任务
        /// </summary>
        /// <param name="inputParam"></param>
        /// <returns></returns>
        public virtual bool IsTrue(BevNodeInputParam inputParam)
        {
            return inputParam.UpId == "0";
            //return Children[0].Evaluate(inputParam);
        }

        public static BevNode Create()
        {
            return new Condition();
        }
    }

    /// <summary>
    /// 接任务条件
    /// </summary>
    class Condition_AND : Condition
    {
        /// <summary>
        /// 两个条件同时满足
        /// </summary>
        /// <param name="inputParam"></param>
        /// <returns></returns>
        public override bool IsTrue(BevNodeInputParam inputParam)
        {
            return Children[0].Evaluate(inputParam) && Children[1].Evaluate(inputParam);
        }

        public static new BevNode Create()
        {
            return new Condition_AND();
        }
    }

    /// <summary>
    /// 等级达到
    /// </summary>
    class Condition_LevelRequirement : Condition
    {
        public static new BevNode Create()
        {
            return new Condition_LevelRequirement();
        }

        protected override bool DoEvaluate(BevNodeInputParam input)
        {
            var taskInfo = Program.GetTaskById(input.TaskId);
            return input.UserLvl >= taskInfo.UserLvl;
        }
    }

    /// <summary>
    /// 上一个任务完成
    /// </summary>
    class Condition_FinishTask : Condition
    {
        public static new BevNode Create()
        {
            return new Condition_FinishTask();
        }

        protected override bool DoEvaluate(BevNodeInputParam input)
        {
            MyTask up = Program.GetMyTaskById(input.UpId);
            return up != null && up.Status == 1;
        }
    }
    #endregion

    #region 更新任务
    /// <summary>
    /// 任务目标
    /// </summary>
    class Objective : BevNode
    {
        public virtual bool IsTrue(BevNodeInputParam inputParam)
        {
            return Children[0].Evaluate(inputParam);
        }

        /// <summary>
        /// 根据入参更新任务状态
        /// </summary>
        /// <param name="inputParam"></param>
        /// <returns></returns>
        public EObjectiveState UpdateValue(BevNodeInputParam inputParam)
        {
            MyTask mq = Program.GetMyTaskById(inputParam.TaskId);
            var taskInfo = Program.GetTaskById(inputParam.TaskId);
            if ((mq.ProgressNum1 >= mq.TargetNum1 && mq.ProgressNum2 >= mq.TargetNum2))
            {
                //接任务2的条件1接收成功
                mq.Status = 1;
                return EObjectiveState.Finished;
            }
            else
            {
                return EObjectiveState.Updated;
            }
        }

        public static BevNode Create()
        {
            return new Objective();
        }
    }
    class Objective_LevelRequirement : Objective
    {
        public static new BevNode Create()
        {
            return new Objective_LevelRequirement();
        }

        protected override bool DoEvaluate(BevNodeInputParam inputParam)
        {
            var taskInfo = Program.GetTaskById(inputParam.TaskId);
            MyTask mt = Program.GetMyTaskById(inputParam.TaskId);
            if (inputParam.UserLvl >= taskInfo.UserLvl)
            {
                mt.ProgressNum1++;
                return true;
            }
            return false;
        }
    }

    #endregion
}
